<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <script src="/common/js/jquery.min.js" type="text/javascript"></script>
    <script src="/common/layui/layui.js" type="text/javascript"></script>
    <script src="/common/layui/layui.all.js" type="text/javascript"></script>
    <script src="/common/js/userServer.js" type="text/javascript"></script>
    <link rel="stylesheet" href="/common/layui/css/layui.css">
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm">用戶列表</button>
<!--                <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">新增用户</button>-->
        <!--        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>-->
    </div>
</script>
<script type="text/html" id="barDemo">
    {{# if(d.state == "1"){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">禁用</a>
    {{#  } }}
    {{# if(d.state == "2"){ }}
    <a class="layui-btn layui-btn-xs" lay-event="edit">解禁</a>
    {{#  } }}
</script>
<script src="/common/js/jquery-2.1.4.js" charset="utf-8"></script>
<script src="/common/layui/layui.js" charset="utf-8"></script>
<script src="/common/js/userServer.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    var col = [
        {type: 'checkbox', fixed: 'left'}
        , {field: 'uid', title: 'ID', sort: true}
        , {field: 'uname', title: '用户名称'}
        , {field: 'xingming', title: '用户姓名'}
        , {field: 'iphone', title: '联系方式'}
        , {field: 'dizhi', title: '地址'}
        , {field: 'utype', title: '用户类型'}
        , {field: 'states', title: '状态'}
        , {fixed: 'right', title: '操作', toolbar: '#barDemo'}
    ];
    layui.use('table', function () {
        var table = layui.table;

        table.render({
            elem: '#test'
            , url: '/getData/userAdmin'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , page: true
            , height: 615
            , request: {
                pageName: 'curr' //页码的参数名称，默认：page
                , limitName: 'nums' //每页数据量的参数名，默认：limit
            }
            , defaultToolbar: ['filter']
            , title: '类型数据表'
            , cols: [
                col
            ]
        });
        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    layer.open({
                        title: '拍品类别添加',
                        area: ['60%', '80%'],
                        type: 2,
                        closeBtn: 2,
                        cancel: function (index, layero) {//关闭回调
                            window.location.href = Base.serverPath+'/user/userAdmin';
                        },
                        content: Base.serverPath+'/user/toRegister',
                    });
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            console.log(data)
            if (obj.event === 'del') {
                if (data.state == 1) {
                    layer.confirm('確定禁用该用户', function (index) {
                        var param = {};
                        param.uid = data.uid;
                        param.state = 2;
                        Base.requestServer("upUState", param, function (data) {
                            if (data == 1) {
                                layer.msg("解禁成功");
                            } else {
                                layer.msg("操作失败，稍后再试");
                            }
                        });
                        setTimeout(function () {
                            window.location.reload();
                        },2000);
                    });
                } else {
                    layer.msg("该用户已处于警用状态")
                }
            } else if (obj.event === 'edit') {
                if (data.state == 2) {
                    layer.confirm('確定解禁该用户', function (index) {
                        var param = {};
                        param.uid = data.uid;
                        param.state = 1;
                        Base.requestServer("upUState", param, function (data) {
                            if (data == 1) {
                                layer.msg("解禁成功");
                            } else {
                                layer.msg("操作失败，稍后再试");
                            }
                        });
                        setTimeout(function () {
                            window.location.reload();
                        },2000);
                    });
                } else {
                    layer.msg("该用户已处于正常状态")
                }
            }
        });
    });
</script>

</body>
</html>